class Stall extends egret.Sprite {
  private _observer = Observer.getInstance();
  private _kitchen: Kitchen = new Kitchen();
  private _hall: Hall;
  private _timer = 0;
  private _money: number = 0;
  private _moneyText: egret.TextField = new egret.TextField();
  private _menus: Food[] = [];
  private _menusText: egret.TextField = new egret.TextField();

  constructor() {
    super();
    this._moneyText.height = 35;
    this._moneyText.width = 200;
    this._moneyText.backgroundColor = 0x000000;
    this._moneyText.textColor = 0xffffff;
    this._menusText.height = 120;
    this._menusText.width = 200;
    this._menusText.backgroundColor = 0x000000;
    this._menusText.textColor = 0xffffff;
    this._menusText.y = this._moneyText.y + 50;

    this._hall = new Hall(this._kitchen);

    this.addChild(this._hall);
    this.addChild(this._moneyText);
    this.addChild(this._menusText);
    this.addChild(this._kitchen);

    this._observer.on(GameEvent.FOOD_CONSUMED, this._onFoodConsumed)
  }


  init() {
    this._kitchen.init();
    this._hall.init();
    this._initMenus();
    this._initMoney();
  }
  // 初始化金钱
  private _initMoney() {
    this._money = 1000;
    this._updateMoneyText();
  }
  // 初始化菜单
  private _initMenus() {
    this._menus.push(
      new Food({
        id: 1,
        name: "鸡蛋饼",
        resKey: "FD1_png",
        price: 5,
        markTime: 10000,
      })
    );
    this._menus.push(
      new Food({
        id: 2,
        name: "汉堡",
        resKey: "FD2_png",
        price: 15,
        markTime: 10000,
      })
    );
    this._updateMenusText();
  }

  private _updateMenusText() {
    let str = "";
    str = this._menus.reduce((str, food) => {
      return str + `${food.name}: 💴 ${food.price}\n`;
    }, str);
    this._menusText.text = str;
  }

  private _updateMoneyText() {
    this._moneyText.text = this._money + "元";
  }

  private _onFoodConsumed = (food: Food) => {
    this._money += food.price;
    this._updateMoneyText();
  }
  /**
   * 餐厅开始营业
   */
  start() {
    // 间隔一段时间招揽一个顾客
    egret.setInterval(this._attractCustomer, this, 5000);
  }

  private _attractCustomer() {
    this._hall.enter(this._createCustomer());
  }

  private _createCustomer() {
    const food = this._getRandomFood();
    return new Customer(food.clone(), "malio_png");
  }

  private _getRandomFood() {
    const randomIdx = Utils.randomRangeInt(0, this._menus.length);
    const food = this._menus[randomIdx];
    return food;
  }
}
